import parse from '@dfeidao/atom-web/dt/parse';
import get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import hide from '@dfeidao/atom-web/ui/hide';
import set_node_cls from '@dfeidao/atom-web/ui/set-node-cls';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	const flag = get('flag');
	const nos = get('nos');
	const exam_name = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_name"]').value;
	const examination_category = fd.data.node.querySelector<HTMLInputElement>('[data-id="examination_category"]').value;
	const exam_score = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_score"]').value;
	const exam_time = fd.data.node.querySelector<HTMLInputElement>('[data-id="exam_time"]').value;
	const exam_explain = fd.data.node.querySelector<HTMLTextAreaElement>('[data-id="exam_explain"]').value;
	const start_time1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="start_time"]').value;
	const finish_time1 = fd.data.node.querySelector<HTMLInputElement>('[data-id="finish_time"]').value;
	const time = new Date();
	const release_time = time.getTime();
	const start_time = parse(start_time1);
	const finish_time = parse(finish_time1);
	const ret = await nodejs<{
		dt: Array<{
			exam_name: string; start_time: number;
			examination_category: string; exam_time: string; exam_score: string; exam_explain: string;
			finish_time: number
		}>
	}>('examination-management/zj-000003/s001', {
		exam_id: nos
	});
	for (let i = 0; i < ret.dt.length; i++) {
		if (exam_name === ret.dt[i].exam_name && start_time === ret.dt[i].start_time && examination_category === ret.dt[i].examination_category && exam_time === ret.dt[i].exam_time && exam_score === ret.dt[i].exam_score && exam_explain === ret.dt[i].exam_explain && finish_time === ret.dt[i].finish_time) {
			return;
		}
	}
	if (!exam_name || !exam_score || !exam_time || !examination_category || !start_time1 || !finish_time1) {
		alert("请完善信息");
		return;
	} else if (start_time1 >= finish_time1) {
		alert("结束时间必须大于开始时间");
		return;
	} else {
		const res = await nodejs<{ code: 1 | 0; result: string }>('examination-management/zj-000003/s002', {
			exam_name,
			examination_category,
			start_time,
			finish_time,
			exam_score,
			exam_time,
			exam_explain,
			release_time,
			exam_id: nos
		});
		if (res.code === 1) {
			alert("修改成功!");
			const addtol = fd.data.node.querySelector<HTMLDivElement>('[data-id="updata"]');
			set_node_cls(addtol, 'display-n', true);
			hide(fd.data.node);
			fire('zj-000001', 'a007');
			if (flag === 1) {
				fire('zj-000001', 'a010');
			} else {
				fire('zj-000001', 'a002');
			}
		} else {
			alert("修改失败了哦" + res.result);
		}
	}
}
